package controller;

import dao.BorrowDaoImpl;
import domain.Borrow;
import domain.BorrowApplication;
import domain.Equipment;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 简介说明: 借用servlet
 *
 * @author: Half_Q_0115
 * @date: 2024/09/18 08:59:57
 * @version: 1.0
 */

@WebServlet("/borrowServlet")
public class BorrowServlet extends HttpServlet {
    private BorrowDaoImpl borrowDao = new BorrowDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String method = req.getParameter("method");
        if (method.equals("addBorrowApplication")) {
            this.addBorrowApplication(req,resp);
        } else if (method.equals("getMyBorrowApplication")) {
            this.getMyBorrowApplication(req,resp);
        } else if (method.equals("getMyBorrow")) {
            this.getMyBorrow(req,resp);
        } else if (method.equals("getBorrowApplicationById")) {
            this.getBorrowApplicationById(req,resp);
        } else if (method.equals("getTeacherBorrowApplication")) {
            this.getTeacherBorrowApplication(req, resp);
        } else if (method.equals("getTeacherBorrow")) {
            this.getTeacherBorrow(req, resp);
        } else if(method.equals("updateBorrowApplicationStatus")){
            this.updateBorrowApplicationStatus(req,resp);
        } else if (method.equals("deleteBorrowById")) {
            this.deleteBorrowById(req, resp);
        } else if (method.equals("getAllBorrow")) {
            this.getAllBorrow(req,resp);
        } else if (method.equals("getAllBorrowApplication")) {
            this.getAllBorrowApplication(req, resp);
        }
        else {
            System.out.println("请求地址错误");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }

    private void addBorrowApplication(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //System.out.println("添加申请");
        String data = req.getParameter("data");
        String user_id = (String) req.getSession().getAttribute("id");;
        Gson gson = new Gson();
        BorrowApplication ba = gson.fromJson(data, BorrowApplication.class);
        ba.setUser_id(user_id);
        //System.out.println(ba);
        int ret = borrowDao.addBorrowApplication(ba);
        PrintWriter writer = resp.getWriter();
        if (ret > 0) {
            writer.print(1);
        }else {
            writer.print(0);
        }
        writer.close();
    }
    private void getMyBorrowApplication(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String user_id = (String) req.getSession().getAttribute("id");;
        List<BorrowApplication> list = borrowDao.findMyBorrowApplication(user_id);
        //System.out.println(list);
        HashMap<String,Object> map = new LinkedHashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",100);
        map.put("data",list);
        Gson gson = new Gson();
        String json = gson.toJson(map);
        System.out.println(json);
        PrintWriter writer =  resp.getWriter();
        writer.print(json);
        writer.close();
    }

    private void getTeacherBorrowApplication(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String user_id = (String) req.getSession().getAttribute("id");;
        List<BorrowApplication> list = borrowDao.findTeacherBorrowApplication(user_id);
        //System.out.println(list);
        HashMap<String,Object> map = new LinkedHashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",100);
        map.put("data",list);
        Gson gson = new Gson();
        String json = gson.toJson(map);
        System.out.println(json);
        PrintWriter writer =  resp.getWriter();
        writer.print(json);
        writer.close();
    }

    private void getAllBorrowApplication(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<BorrowApplication> list = borrowDao.findAllBorrowApplication();
        //System.out.println(list);
        HashMap<String,Object> map = new LinkedHashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",100);
        map.put("data",list);
        Gson gson = new Gson();
        String json = gson.toJson(map);
        System.out.println(json);
        PrintWriter writer =  resp.getWriter();
        writer.print(json);
        writer.close();
    }

    private void getMyBorrow(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String user_id = (String) req.getSession().getAttribute("id");;
        List<Borrow> list = borrowDao.findMyBorrow(user_id);
        //System.out.println(list);
        HashMap<String,Object> map = new LinkedHashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",100);
        map.put("data",list);
        Gson gson = new Gson();
        String json = gson.toJson(map);
        System.out.println(json);
        PrintWriter writer =  resp.getWriter();
        writer.print(json);
        writer.close();
    }

    private void getTeacherBorrow(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String user_id = (String) req.getSession().getAttribute("id");;
        List<Borrow> list = borrowDao.findTeacherBorrow(user_id);
        //System.out.println(list);
        HashMap<String,Object> map = new LinkedHashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",100);
        map.put("data",list);
        Gson gson = new Gson();
        String json = gson.toJson(map);
        System.out.println(json);
        PrintWriter writer =  resp.getWriter();
        writer.print(json);
        writer.close();
    }
    private void getAllBorrow(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<Borrow> list = borrowDao.findAllBorrow();
        //System.out.println(list);
        HashMap<String,Object> map = new LinkedHashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",100);
        map.put("data",list);
        Gson gson = new Gson();
        String json = gson.toJson(map);
        System.out.println(json);
        PrintWriter writer =  resp.getWriter();
        writer.print(json);
        writer.close();
    }

    private void getBorrowApplicationById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String user_id = (String) req.getSession().getAttribute("id");;
        String equipment_id = req.getParameter("equipment_id");
        BorrowApplication ba = borrowDao.findBorrowApplicationByid(equipment_id, user_id);
        //System.out.println(ba);
        HashMap<String,Object> map = new LinkedHashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",100);
        map.put("data",ba);
        Gson gson = new Gson();
        String json = gson.toJson(map);
        //System.out.println(json);
        PrintWriter writer =  resp.getWriter();
        writer.print(json);
        writer.close();
    }

    protected void updateBorrowApplicationStatus(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String equipmentId = request.getParameter("equipment_id");
        String approvalStatus = request.getParameter("approval_status");
        PrintWriter out = response.getWriter();
        int result = borrowDao.updateApprovalStatus(equipmentId, approvalStatus);
        if (result > 0) {
            out.print(1);
        } else {
            out.print(0);
        }
    }

    protected void deleteBorrowById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String equipmentId = req.getParameter("equipment_id");
        PrintWriter out = resp.getWriter();
        int result = borrowDao.deleteBorrowById(equipmentId);
        if (result > 0) {
            out.print(1);
        }else {
            out.print(0);
        }
    }
}
